package com.studerb.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import org.jdesktop.application.SingleFrameApplication;

import javax.swing.ActionMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;

import org.apache.log4j.Logger;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Gnusbin extends SingleFrameApplication {
	private static Logger log = Logger.getLogger(Gnusbin.class);
	private AbstractApplicationContext appContext;
	private JFrame mainFrame = null;
	private StatusBar statusBar = null;

	@Override protected void startup() {
		log.debug("Startup of SingleFrameApplication");
		startupSpring();
		startupGui();
	}

	private void startupSpring() {
		log.debug("Starting Spring Application Context");
		this.appContext = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
		this.appContext.registerShutdownHook();
		this.appContext.start();
	}

	private void startupGui() {
		statusBar = new StatusBar(this, getContext().getTaskMonitor());
		mainFrame = getMainFrame();
		mainFrame.setJMenuBar(createMenuBar());
		mainFrame.add(createToolBar(), BorderLayout.NORTH);
		mainFrame.add(createMainPanel(), BorderLayout.CENTER);
		mainFrame.add(statusBar, BorderLayout.SOUTH);
		show(mainFrame);
	}

	private ActionMap actionMap() {
		return getContext().getActionMap();
	}

	private JMenu createMenu(String menuName, String[] actionNames) {
		JMenu menu = new JMenu();
		menu.setName(menuName);
		for (String actionName : actionNames) {
			JMenuItem menuItem = new JMenuItem();
			menuItem.setAction(actionMap().get(actionName));
			menu.add(menuItem);
		}
		return menu;
	}

	private JMenuBar createMenuBar() {
		String[] demoMenuActionNames = { "blockAction", "blockComponent", "blockApplication", "blockWindow", "quit" };
		JMenuBar menuBar = new JMenuBar();
		menuBar.add(createMenu("demoMenu", demoMenuActionNames));
		return menuBar;
	}

	private JComponent createToolBar() {
		String[] toolbarActionNames = { "blockAction", "blockComponent", "blockApplication", "blockWindow", };
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		for (String actionName : toolbarActionNames) {
			JButton button = new JButton();
			button.setRequestFocusEnabled(false);
			button.setAction(actionMap().get(actionName));
			button.setVerticalTextPosition(JButton.BOTTOM);
			button.setHorizontalTextPosition(JButton.CENTER);
			button.setName(actionName + "ToolBarButton");
			toolBar.add(button);
		}
		return toolBar;
	}

	private JComponent createMainPanel() {
		JButton actionButton = new JButton(actionMap().get("blockAction"));
		JButton componentButton = new JButton(actionMap().get("blockComponent"));
		JButton applicationButton = new JButton(actionMap().get("blockApplication"));
		JButton windowButton = new JButton(actionMap().get("blockWindow"));
		JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 50));
		panel1.add(actionButton);
		panel1.add(componentButton);
		panel1.add(applicationButton);
		panel1.add(windowButton);
		JPanel panel2 = new JPanel(new BorderLayout());
		panel2.add(new JSeparator(), BorderLayout.NORTH);
		panel2.add(panel1, BorderLayout.CENTER);
		panel2.setBorder(new EmptyBorder(0, 2, 0, 2)); // top, left, bottom, right
		return panel2;
	}

	public static void main(String[] args) {
		try {
			SingleFrameApplication.launch(Gnusbin.class, args);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
			System.exit(1);
		}
	}
}